#!/bin/sh


BOOGIE_DIR="boogie"
Z3_DIR="z3"
DIST="dist"
BOOGIE_DEST_DIR="$DIST/boogie+z3"
Z3_DEST_DIR="$BOOGIE_DEST_DIR/Z3"


OS=$1

echo ' 
  ====================================================================
     ____                    _           __________ 
    | __ )  ___   ___   __ _(_) ___   _ |__  /___ / 
    |  _ \ / _ \ / _ \ / _` | |/ _ \_| |_ / /  |_ \ 
    | |_) | (_) | (_) | (_| | |  __/_   _/ /_ ___) |
    |____/ \___/ \___/ \__, |_|\___| |_|/____|____/ 
                    |___/      
  ====================================================================
'
##
## we only know these two platforms
##
if [ "$OS" != "LINUX" -a "$OS" != "OSX" ]; then
    echo "usage: build.sh [OSX|LINUX]"
    exit
fi

# start by cleaning out the DIST directory
echo "Cleaning up distribution directory..."
rm -fr $DIST



##
## start by building z3
##
if [ ! -d $Z3_DIR ]; then
    echo "Z3 Directory does not exist. Can't continue."
    exit
fi


FULL_PATH=`pwd`/$Z3_DEST_DIR

mkdir -p $FULL_PATH

cd $Z3_DIR
autoconf && ./configure --prefix=$FULL_PATH && python scripts/mk_make.py && cd build

make && sudo make install

if [ $? -ne 0 ]; then
    echo "Build of Z3 Failed!"
    exit
fi

cd ../../

##
## Build BOOGIE
##

if [ ! -d $BOOGIE_DIR ]; then
    echo "Boogie Directory does not exist. Can't continue."
    exit
fi


cd $BOOGIE_DIR/Source

xbuild Boogie.sln


if [ $? -ne 0 ]; then
    echo "Build of Boogie Failed!"
    exit
fi

cd ../../

##
## make the distribution
##

echo "Creating the distribution"
cp -r $BOOGIE_DIR/* $BOOGIE_DEST_DIR/

##
## Create the symbolic link
##


cd $BOOGIE_DEST_DIR/Binaries/

ln -s ../Z3/bin/z3 z3.exe

echo "Build Complete. Please find your build in the dist/boogie+z3 directory"
